package com.linkcard.list.persistence;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkcard.commen.tools.Page;
import com.linkcard.list.model.Business;

public class BusinessDaoImpl extends HibernateDaoSupport implements BusinessDao {

	//添加商家
	public boolean addBusiness(Business business) {
		try
		{
			this.getHibernateTemplate().save(business);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//获得商家列表
	public List businessList(final Page pageObj, final String searchStr) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Business business,Category category where business.categoryId=category.id ";
				if(!searchStr.equals(""))
				{
					String[] str=searchStr.split("_");
					if(!str[0].equals("-2"))
					{
					  hql+=" and business.categoryId="+str[0];
					}
					if(!str[1].equals("-2"))
					{
					  hql+=" and business.subCategoryId="+str[1];
					}
					if(str.length==3)
					{
					hql+=" and (business.name like '%"+str[2]+"%'";
					hql+=" or business.introduction like '%"+str[2]+"%')";
					}
				}
				hql+=" order by business.datetime desc ";
				int rowCount = getRowCount(hql);
				  Query query=session.createQuery(hql);
				  if(pageObj!=null)
				  {
				  query.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				  query.setMaxResults(pageObj.getPageSize());
		         pageObj.setRowCount(rowCount);
		         pageObj.setTotalPage(rowCount);
		         pageObj.setPreNextPage();
				  }
				return query.list();
			}
		});
		return list;
	}
	/**
	 * 获得记录数
	 */
	public int getRowCount(final String hql) {
		Integer rowCount = 0;
		rowCount = (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(hql).list().size();
					}
				});
		return rowCount;
	}

	//查找商家
	public Business findBusiness(Integer id) {
		Business business=null;
		try
		{
		 business=(Business)this.getHibernateTemplate().get(Business.class, id);
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return business;
	}

	//删除商家
	public boolean deleteBusiness(final String ids) {
		boolean isSuccess=(Boolean)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				try
				{
				String hql="delete from Business business where business.id in("+ids+")";
				//hql="delete from Business business where business.id in(select business.id from Business business where business.id in("+ids+"))";
				Query query =session.createQuery(hql);
				query.executeUpdate();
				}catch(Exception e)
				{
					e.printStackTrace();
					return false;
				}
				return true;
			}
			
		});
		return isSuccess;
	}

	//修改商家
	public boolean modifyBusiness(Business business) {
		try
		{
			Business b=( Business)this.getHibernateTemplate().get(Business.class, business.getId());
			business.setDatetime(b.getDatetime());
			BeanUtils.copyProperties(b, business);
			this.getHibernateTemplate().update(b);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
		return true;
	}

	//推荐商家
	public List businessListRecommend(final Page pageObj,final int recommend) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Business business where business.recommend="+recommend;
				hql+=" order by business.datetime desc ";
				
				  Query query=session.createQuery(hql);
				  if(pageObj!=null)
				  {
				  query.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				  query.setMaxResults(pageObj.getPageSize());
				  }
				return query.list();
			}
		});
		return list;
	}

	//获得分店商家
	public List branchBusinessList(final String pname) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Business business,Category category where business.categoryId=category.id ";
				hql+=" and business.pname='"+pname+"'";
				  Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return list;
	}

}
